package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvName;
    private TextView tvPwd;
    private EditText etName;
    private EditText etPwd;
    private Button btnSub;
    private Button btnZhuce;
    private static int requestcode = 65;

    private Boolean flag = false;


    String user;
    String pwd;
    String age;
    String trueName;
    private String info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvName = (TextView) findViewById(R.id.tv_name);
        tvPwd = (TextView) findViewById(R.id.tv_pwd);
        etName = (EditText) findViewById(R.id.et_name);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        btnSub = (Button) findViewById(R.id.btn_sub);
        btnSub.setOnClickListener(this);
        btnZhuce = (Button) findViewById(R.id.btn_zhuce);
        btnZhuce.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_sub:
                if (!flag){
                    Toast.makeText(MainActivity.this, "账号不存在", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent result = new Intent(MainActivity.this,ThirdActivity.class);
                result.putExtra("info",info);
                startActivity(result);
                break;
            case R.id.btn_zhuce:
                flag = true;
                Intent zhuce = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(zhuce, requestcode);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == requestcode) {
            if (resultCode == Activity.RESULT_OK) {
                user = data.getStringExtra("user");
                 pwd = data.getStringExtra("pwd");
                 age = data.getStringExtra("age");
                 trueName = data.getStringExtra("trueName");
                info = "账号：" + user + "\n密码：" + pwd + "\n年龄：" + age + "\n姓名：" + trueName;
                etName.setText(user);
                etPwd.setText(pwd);
            }
        }
    }
}